import os
import gc
from pyglet import window
from pyglet import clock
from pyglet import gl
from pyglet import image
from pyglet.window import key
from pyglet.media import Player
import random
# our own files
from sprite import *
from player import *
from buffalo import *
from bird import *
from bear import *
from cactus import *
from goal import *
from spawner import *
from common import *

class GameWindow(window.Window):
	
	def __init__(self, *args, **kwargs):
		# init the window
		window.Window.__init__(self, *args, **kwargs)
		
		self.offset = 0
		
		# creates the aim
		self.set_mouse_visible(False)
		self.mouse_x = 400
		self.mouse_y = 300
		self.aim = load_image("aim.png")
		
		# load menu screen
		self.menu_image = load_image("menu.jpg")
		# load victory screen
		self.victory_image = load_image("victory.png")
		# load game over screen
		self.game_over_image = load_image("game_over.png")
		
		# go to menu timer
		self.count_down = 4
		
		# init game
		self.running = False
		self.init_game()
		
		# music!
		self.player = Player()
		self.player.queue(load_music("99801_Evoluid___Mountain_Chief.mp3"))
		self.player.eos_action = Player.EOS_LOOP
		self.player.play()
	
	def init_game(self):
		print(1)
		self.count_down = 4
		Game.sprites = []
		Game.feathers = []
		print(2)
		Game.game_over = False
		Game.victory = False
		self.player = Indian(x=100, y=500)
		print(3)
		Game.sprites.append(self.player)
		print(4)
		Game.sprites.append(Buffalo(x = 200))
		print(5)
		Game.sprites.append(Bear(x = 600))
		print(6)
		Game.sprites.append(Cactus("pit.png", x = 560, dammage = 100))
		Game.sprites.append(Cactus("tall_cactus.png", x = 880))
		Game.sprites.append(Cactus("small_cactus.png", x = 1560))
		Game.sprites.append(Cactus("tall_cactus.png", x = 2050, y = 300))
		Game.sprites.append(Cactus("small_cactus.png", x = 3520))
		print(8)
		Game.sprites.append(Spawner("buffalo", x = 810))
		Game.sprites.append(Spawner("buffalo", x = 1600))
		print(9)
		Game.sprites.append(Spawner("bird", x = 3700))
		Game.sprites.append(Spawner("bird", x = 1800))
		print(10)
		Game.sprites.append(Spawner("bear", x = 3200))
		print(11)
		for i in range(0, 10):
			Game.sprites.append(Bird(x = random.randint(100, 3900)))
		print(12)
		Game.sprites.append(Goal(x=3700))
		print(13)
		# there is most likely a lot of garbage here, if the game is run more than one time
		#gc.collect()
		
	
	def main_loop(self):
		
		clock.set_fps_limit(30);
		
		while not self.has_exit:
			self.dispatch_events()
			
			dt = clock.tick()
			
			self.update(dt)
			self.draw()
			self.flip()
		
		print("Goodbye :P")
	
	def update(self, dt):
		if self.running:
			update_stats(dt)
			to_remove = []
			if (Game.game_over or Game.victory):
				self.count_down -= dt
				if (self.count_down < 0):
					self.running = False
					return
			for sprite in Game.sprites:
				if (sprite.delete):
					to_remove.append(sprite)
					continue
				sprite.update(dt)
			for sprite in to_remove:
				Game.sprites.remove(sprite)
	
	def draw(self):
		self.clear()
		gl.glEnable(gl.GL_BLEND)
		gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
		gl.glDisable(gl.GL_DEPTH_TEST)
		
		if not self.running:
			self.menu_image.blit(0, 0)
		else:
			self.offset = self.player.x - 400
			if self.offset < 0:
				self.offset = 0
			if self.offset > map.get_width() - 800:
				self.offset = map.get_width() - 800
			
			map.draw(self.offset)
			draw_stats()
			
			for sprite in Game.sprites:
				sprite.draw(self.offset)
			self.aim.blit(self.mouse_x - self.aim.width / 2, self.mouse_y - self.aim.height / 2)
			
			if (Game.game_over):
				self.game_over_image.blit(0,0)
			elif (Game.victory):
				self.victory_image.blit(0,0)
	
	#
	#     Event listeners
	#
	def on_key_press(self, symbol, modifiers):
		if symbol == key.ESCAPE:
			if (self.running):
				if (Game.game_over or Game.victory):
					self.running = False
				else:
					self.player.die()
					self.count_down = 0.5
			else:
				self.has_exit = True
		elif not self.running:
			self.running = True
			self.init_game()
		elif symbol in (key.RIGHT, key.D):
			self.player.go_right(True)
		elif symbol in (key.LEFT, key.A):
			self.player.go_left(True)
		elif symbol in (key.SPACE, key.UP, key.W):
			self.player.jump()
	
	def on_key_release(self, symbol, modifiers):
		if symbol in (key.RIGHT, key.D):
			self.player.go_right(False)
		elif symbol in (key.LEFT, key.A):
			self.player.go_left(False)
	
	
	def on_mouse_motion(self, x, y, dx, dy):
		if self.running:
			self.mouse_x = x
			self.mouse_y = y
			self.player.aim(self.offset, x, y)
	
	def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
		if self.running:
			self.mouse_x = x
			self.mouse_y = y
			self.player.aim(self.offset, x, y)

	def on_mouse_press(self, x, y, button, modifiers):
		if not self.running:
			self.running = True
			self.init_game()
		elif button == window.mouse.LEFT:
			self.player.shoot(self.offset, x, y)

if __name__ == "__main__":
	# someone launched the file
	game = GameWindow(width=800, height=600)
	game.main_loop()
